home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5227 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  70 lines

  1. Path: news.nstn.ca!news
  2. From: nstn2264@fox.nstn.ca (Robin Murray)
  3. Newsgroups: comp.lang.c++
  4. Subject: strstream and string::read_to_delim()
  5. Date: Sat, 03 Feb 1996 04:17:43 GMT
  6. Organization: Nova Scotia Technology Network
  7. Message-ID: <4euk0k$pkg@news.nstn.ca>
  8. NNTP-Posting-Host: ts1-05.mon.inforamp.net
  9. X-Newsreader: Forte Free Agent v0.46
  10.  
  11. i've spent the last two days carving out a piece of problem code and 
  12. narrowing it down to the following short program. what i want to 
  13. do is extract newline-terminated strings (\r\n) into string classes
  14. using ansi string::read_to_delim(strstream). once i read the last
  15. string in, i want to then reset the strstream to put the get/put 
  16. pointers back to the beginning of the stream, and place a null 
  17. terminator at the beginning. further calls to read_to_delim will 
  18. then (hopefully) return a null string.
  19.  
  20. using bc++ 4.02, this doesn't always happen. when the string reaches 
  21. 64 chars in length or more, i get wierd results. the first 
  22. read_to_delim works fine. i set the get/put pointers to 0, and insert
  23. a  null terminator. then the second call to read_to_delim returns
  24. chars  from 64+ to the end of the string, instead of a null string.
  25.  
  26. can anyone tell me why? thanks...
  27.  
  28. #include <iostream.h>
  29. #include <strstrea.h>
  30. #include <cstring.h>
  31.  
  32. int main()
  33. {
  34.   strstream streamTest; string stringTest1, stringTest2;
  35.  
  36.   // the following works...
  37.   // streamTest << "ABCDEFGH\r\n" << ends;
  38.   // the following fails...
  39.   streamTest <<
  40. "testingtestingtestingabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n"
  41. << ends;
  42.   // the following fails...
  43.   // streamTest <<
  44. "testingtestingtestingabcdefghijklmnopqrstuvwxyz1234567890ABCDEF\r\n"
  45. << ends;
  46.   // the following works...only one shorter that the one above
  47.   // streamTest <<
  48. "testingtestingtestingabcdefghijklmnopqrstuvwxyz1234567890ABCDE\r\n"
  49. << ends;
  50.   // the following works...when there is no \r\n
  51.   // streamTest <<
  52. "testingtestingtestingabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  53. << ends;
  54.  
  55.   // stringTest1 get up to the "\n", leaves "\n" in the stream
  56.   stringTest1.read_to_delim( streamTest );
  57.   // reset stream to the beginning, and put eof at beginning of stream
  58.   streamTest.rdbuf()->seekpos( 0 );
  59.   streamTest << ends;
  60.   // this SHOULD produce a null string. doesn't all the time...
  61.   stringTest2.read_to_delim( streamTest );
  62.   return 0;     // set a break point here, and inspect "stringTest2"
  63. }
  64.  
  65. --
  66. Robin Murray
  67. Moncton, New Brunswick
  68. 506-853-3779
  69.  
  70.